//app.js
var md5 = require('utils/lib/md5.js');
var fun_base64 = require('utils/lib/base64.js');
var utils = require('utils/shop/formatShop.js');
var xml2json = require('utils/lib/xml2json');

App({
  onLaunch: function() {
    var appConfig = wx.getExtConfigSync();
    this.globalData.appConfig = appConfig;
    wx.showLoading({
      title: '加载中...'
    });
    this.globalData.init = {
      appid: 'wxcc87e75f0631c98f',
      mch_id: '1547098521',
      api_key: 'bw4b576hbw456hbw4576w45btw45twer',
      front_url: 'https://mall.dlceb.com',
      notify_url: '/pay/wxpay2/notify_url.php',

      //购买分销商微信回调
      notify_distribute: '/pay/wxpay2/notify_distribute.php',
    };

    wx.clearStorageSync();

    //获取商家配置信息
    this.getShopConfig();
  },

  /**
   * 页面分享外加ownerid
   */
  share: function(part = '?') {
    return (part == '?' ? '?' : '&') + 'ownerid=' + (this.globalData.userInfo ? this.globalData.userInfo.User_ID : 0);
  },

  //分享内容
  shareInfo: function(option) {
    var title = '';
    var path = '';
    if (option && option.title && option.path) { //详情页分享（detail、detail_pt、cantuan）
      title = option.title;
      path = option.path;
    } else { //首页分享
      title = this.globalData.shopConfig.ShopName ? this.globalData.shopConfig.ShopName : '我的小店';
      path = '/pages/index/index' + this.share();
    }

    if (title != '' && path != '') {
      return {
        title: title,
        path: path,
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '缺少必要参数',
        showCancel: false
      });
    }
  },

  //客服
  kefu: function(that) {
    //将ownerid写入页面，方便给客服发送的卡片携带参数
    // that.setData({
    //   ownerid: this.globalData.ownerid
    // });
    var kefu_animationData = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease',
    });
    //弹出文字
    setTimeout(function() {
      kefu_animationData.width('420rpx').height('auto').translateX(-1).step();
      kefu_animationData.translateX(-11).opacity(1).step();
      that.setData({
        kefu_animationData: kefu_animationData.export()
      });
      //隐藏弹出文字
      setTimeout(function() {
        kefu_animationData.translateX(0).opacity(0).step();
        kefu_animationData.width(0).height(0).step();
        that.setData({
          kefu_animationData: kefu_animationData.export()
        });
      }.bind(that), 3000);
    }.bind(that), 2000);
  },

  //获取店铺配置
  getShopConfig: function(callback = '') {
    var ShopConfig = {
      act: 'shopconfig'
    };
    this.http_req(ShopConfig, this.globalData.init.api_url, 'POST', this.ShopConfig, callback);
  },

  //获取店铺的配置回调
  ShopConfig: function(res, callback = '') {
    if (res.expiresTime) {
      this.globalData.expiresTime = res.expiresTime;
    }

    if (res.errorCode == 0) {
      this.globalData.shopConfig = res.data;
      callback != '' && callback();
    } else {
      wx.showModal({
        title: '提示',
        content: res.msg,
        showCancel: false
      });
    }
  },

  //获取店铺首页配置    that: 调取此方法的页面
  getShopHome: function(that) {
    var shopHomeArgs = {
      act: 'shophome'
    };
    this.http_req(shopHomeArgs, this.globalData.init.api_url, 'POST', that.shopHome);
  },

  /**
   * 检查缓存或重新登录
   * @action: reLogin: 重新登录  getSession: 获取缓存  空： 优先查询缓存，没有缓存则执行登录
   */
  login: function(action, callback, arg = "") {
    if (!arg) {
      return;
    } else if (!this.globalData.userInfo && action == '' && arg != 'login') {
      if (arg == 'cart' || arg == 'distribute' || arg == 'address_list' || arg == 'user_money' || arg == 'jifen' || arg == 'offline_order' || arg == 'orderlist') {
        wx.navigateTo({
          url: '/pages/userlogin/userlogin?arg=' + arg,
        })
      } else {
        wx.navigateTo({
          url: '/pages/userlogin/userlogin',
        })
      }
      return;
    }
    var action_value = 'reLogin,getSession';
    var action = typeof action != 'undefined' && action_value.indexOf(action) > -1 ? action : '';

    if (callback) {
      this.globalData.callback = callback;
    }

    // if (this.globalData.expiresTime < (new Date().getTime() / 1000)) {
    //   wx.hideLoading();
    //   wx.showModal({
    //     title: '提示',
    //     content: '商城已过期，请去续费',
    //     showCancel: false
    //   });
    //   return false;
    // }

    if (action == 'getSession') {
      //查询用户缓存信息
      this.globalData.userInfo = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo') : null;
      this.globalData.callback && this.globalData.callback();
      this.globalData.callback = null;
    } else if (action == 'reLogin') {
      //重新登录
      this.wxLogin();
    } else {
      wx.checkSession({
        success: res => {
          if (wx.getStorageSync('userInfo')) {
            this.globalData.userInfo = wx.getStorageSync('userInfo');
            this.globalData.callback && this.globalData.callback();
            this.globalData.callback = null;
          } else {
            this.wxLogin();
          }
        },
        fail: () => {
          this.wxLogin();
        }
      });
    }
  },

  //------------会员登录  start--------------//
  //调取微信登录接口,获取sessionKey
  wxLogin: function() {
    wx.removeStorageSync('userInfo');
    wx.removeStorageSync('unique_key');
    wx.login({
      success: res => {
        if (res.code) {
          var transData = {
            act: 'login',
            code: res.code
          };
          this.http_req(transData, this.globalData.init.api_url, 'POST', this.userInfo);
        }
      },
      fail: function(res) {
        wx.showToast({
          title: typeof res.errMsg != 'undefined' ? res.errMsg : '服务器错误,请稍后再试',
          icon: 'loading'
        })
      }
    })
  },

  /**
   * 用来调用wx.login接口之后请求服务端数据查询对应的会员数据回调函数
   */
  userInfo: function(res) {
    var that = this;
    var back_data = res;
    if (res.errorCode === 1) {
      wx.setStorageSync('unique_key', res.data);
      wx.getUserInfo({
        success: userInfo => {
          userInfo['unique_key'] = res.data;
          userInfo['ownerid'] = that.globalData.ownerid;
          userInfo['act'] = 'reg';
          that.http_req(userInfo, that.globalData.init.api_url, 'POST', that.regUserInfo);
        },
        fail: function() {
          wx.getSetting({
            success: (res) => {
              let authSetting = res.authSetting || {};
              if (typeof authSetting['scope.userInfo'] === 'undefined') { // 未授权过（第一次授权）
                wx.navigateTo({
                  url: '/pages/userlogin/userlogin',
                })
              } else if (!authSetting['scope.userInfo']) { // 拒绝授权
                wx.showModal({
                  title: '提示',
                  content: '若拒绝授权，则不能使用该小程序的一些功能，点击确定去开启授权',
                  showCancel: false,
                  complete: function(res) {
                    wx.openSetting({
                      success: function(res) {
                        if (res.authSetting['scope.userInfo']) {
                          that.userInfo(back_data);
                        }
                      },
                      fail: function(res) {

                      }
                    })
                  }
                })
              }
            }
          })
        }
      })
    } else if (res.errorCode === 0) {
      wx.setStorageSync('userInfo', res.data);
      that.globalData.userInfo = res.data;
      that.globalData.callback && that.globalData.callback();
      that.globalData.callback = null;

      // 处理分销商
      that.deal_distribute();
    } else {
      wx.showToast({
        title: '服务器错误',
        icon: 'loading'
      })
    }
  },

  //处理分销商逻辑
  deal_distribute() {
    if (!this.globalData.userInfo) return false;
    var deal_distribute_data = {
      'UsersID': this.globalData.userInfo.Users_ID,
      'User_ID': this.globalData.userInfo.User_ID,
      'type': 'distribute'
    };
    this.http_req(deal_distribute_data, this.globalData.init.api_url, 'POST');
  },

  //如果openid或者unionid都没有查询到数据,就会进行注册然后返回注册的信息
  regUserInfo: function(res) {
    if (res.errorCode === 0) {
      wx.setStorageSync('userInfo', res.data);
      this.globalData.userInfo = res.data;
      this.globalData.callback && this.globalData.callback();
      this.globalData.callback = null;

      // 处理分销商
      this.deal_distribute();
    } else {
      wx.showToast({
        title: res.msg,
        icon: 'loading'
      })
    }
  },
  //------------会员登录  end--------------//

  //------------订单操作  start--------------//
  /**
   * 订单操作
   * @action  pay_checkout: 提交订单页支付  pay_orderlist：订单列表页支付  confirm：确认收货
   * @postData  传递过来的数据   {Order_ID(订单ID)}
   */
  orderAction: function(action, postData, callback) {
    var that = this;
    var action_value = 'pay_checkout,pay_orderlist,confirm';
    var action = typeof action != 'undefined' && action_value.indexOf(action) > -1 ? action : '';

    if (action == '' || !postData || postData.Order_ID == '') {
      wx.showModal({
        title: '提示',
        content: '请勿非法提交',
        showCancel: false
      });
      return false;
    }
    //记录订单操作
    if (action) {
      that.globalData.order_action = action;
      that.globalData.order_type = postData.Order_Type ? postData.Order_Type : 'shop';
    }
    //记录回调函数
    if (callback) {
      that.globalData.callback = callback;
    }

    //弹窗
    postData.act = 'get_order';
    postData.User_ID = this.globalData.userInfo.User_ID;
    if (action == 'confirm') {
      wx.showModal({
        title: '提示',
        content: '是否确认收货，收货后不能退货',
        success: function(res) {
          if (res.confirm) { //确认收货
            wx.showLoading({
              title: '确认收货中...'
            });
            //获取订单详情
            that.http_req(postData, that.globalData.init.api_url, 'POST', that.getOrder_callback);
          } else if (res.cancel) { //取消
            that.change_orderlist_variable();
          }
        },
        fail: function() {
          that.change_orderlist_variable();
        }
      });
    } else {
      //获取订单详情
      that.http_req(postData, that.globalData.init.api_url, 'POST', that.getOrder_callback);
    }
  },

  //获取订单详情回调
  getOrder_callback: function(res) {
    var that = this;
    var order_action = that.globalData.order_action;
    if (res.errorCode == 0) {
      var orderInfo = res.data[0];
      if (order_action == 'pay_checkout' || order_action == 'pay_orderlist') { //支付
        if (orderInfo['Order_Status'] == 0) {
          if (order_action == 'pay_orderlist') {
            that.change_orderlist_variable();
          }
          wx.showModal({
            title: '提示',
            content: '该订单需商家确认，请等待确认！',
            showCancel: false,
            success: function() {
              wx.redirectTo({
                url: '/pages/orderlist/orderlist?order_type=' + orderInfo.Order_Type
              })
            }
          });
          return false;
        } else if (orderInfo['Order_Status'] != 1) { //不是待支付状态
          if (order_action == 'pay_orderlist') {
            that.change_orderlist_variable();
          }
          wx.showModal({
            title: '提示',
            content: '该订单不是待付款状态，不能支付',
            showCancel: false
          });
          return false;
        } else {
          var postData = {
            Order_ID: orderInfo['Order_CreateTime'] + orderInfo['Order_ID'],
            totalPrice: orderInfo['Order_TotalPrice'],
            Order_Type: orderInfo['Order_Type']
          };
          that.unifiedorder(postData);
        }
      } else if (order_action == 'confirm') { //确认收货
        if (orderInfo['Order_Status'] != 3) { //不是待收货状态
          wx.hideLoading();
          that.change_orderlist_variable();
          wx.showModal({
            title: '提示',
            content: '该订单不是待收货状态，不能确认收货',
            showCancel: false
          });
          return false;
        } else {
          var postData = {
            UsersID: that.globalData.userInfo.Users_ID,
            User_ID: that.globalData.userInfo.User_ID,
            action: orderInfo.Order_Type === 'kanjia' ? 'kanjia_receive' : 'confirm_receive',
            Order_ID: orderInfo['Order_ID'],
            type: 'confirm'
          };
          this.http_req(postData, that.globalData.init.api_url, 'POST', that.confirmReceive);
        }
      }
    } else {
      if (order_action == 'pay_orderlist' || order_action == 'confirm') {
        that.change_orderlist_variable();
      }
      wx.showModal({
        title: '提示',
        content: res.msg,
        showCancel: false
      })
    }
  },

  //统一下单API
  unifiedorder: function(postData) {
    var Order_Type = postData.Order_Type; //订单类型，区分成为分销商分销订单 和 商城订单
    var notify_url = this.globalData.init.notify_url; //异步回调地址
    switch (Order_Type) {
      //购买分销商
      case 'distribute':
        notify_url = this.globalData.init.notify_distribute
        break;
    }
    // 添加域名
    notify_url = notify_url.indexOf('http') > -1 ? notify_url : this.globalData.init.front_url + notify_url;

    var appid = this.globalData.init.appid; //小程序的appid
    var body = '微商城在线付款';
    var mch_id = this.globalData.init.mch_id; //商户号
    var nonce_str = utils.makeid(); //随即字符串
    var openid = this.globalData.userInfo.User_OpenID; //openid
    var out_trade_no = postData.Order_ID; //商家订单号
    var spbill_create_ip = '123.206.7.101';
    var total_fee = parseInt(postData.totalPrice * 100); //产品总价格
    var api_key = this.globalData.init.api_key; //商户APIkey
    var sign = utils.paysignjsapi(appid, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, api_key);


    //组装xml
    var bodyData = '<xml>';
    bodyData += '<appid>' + appid + '</appid>'; // 小程序ID
    bodyData += '<body>' + body + '</body>'; // 商品描述
    bodyData += '<mch_id>' + mch_id + '</mch_id>'; // 商户号
    bodyData += '<nonce_str>' + nonce_str + '</nonce_str>'; // 随机字符串
    bodyData += '<notify_url>' + notify_url + '</notify_url>'; // 支付成功的回调地址 
    bodyData += '<openid>' + openid + '</openid>'; // 用户标识
    bodyData += '<out_trade_no>' + out_trade_no + '</out_trade_no>'; // 商户订单号
    bodyData += '<spbill_create_ip>' + spbill_create_ip + '</spbill_create_ip>'; // 终端IP
    bodyData += '<total_fee>' + total_fee + '</total_fee>'; // 总金额 单位为分
    bodyData += '<trade_type>JSAPI</trade_type>'; // 交易类型 小程序取值如下：JSAPI
    bodyData += '<sign>' + sign + '</sign>';
    bodyData += '</xml>';
    //统一下单URL
    var urlStr = this.globalData.init.unifiedorder_url;

    wx.request({
      url: urlStr,
      method: 'POST',
      data: bodyData,
      success: res => {
        var xml = xml2json(res.data).xml;
        if (typeof xml == 'undefined' || typeof xml.return_code == 'undefined' || xml.return_code['#text'] == 'FAIL' || typeof xml.result_code == 'undefined' || xml.result_code['#text'] == 'FAIL') {
          if (this.globalData.order_action == 'pay_orderlist') {
            this.change_orderlist_variable();
          }
          var msg = '支付调取失败';
          if (typeof xml.return_code != 'undefined' && xml.return_code['#text'] == 'FAIL') {
            msg = typeof xml.return_msg['#text'] != 'undefined' ? xml.return_msg['#text'] : '支付调取失败';
          } else if (typeof xml.result_code != 'undefined' && xml.result_code['#text'] == 'FAIL') {
            msg = typeof xml.err_code_des['#text'] != 'undefined' ? xml.err_code_des['#text'] : '支付调取失败';
          }

          wx.showModal({
            title: '提示',
            content: msg,
            showCancel: false
          })
        } else {
          var wxpayPackage = 'prepay_id=' + xml.prepay_id['#text'];
          var timestamp = parseInt(new Date().getTime() / 1000).toString();
          var paySign = utils.paysignjs(appid, nonce_str, wxpayPackage, 'MD5', timestamp, api_key);
          var transData = {
            timeStamp: timestamp,
            nonceStr: nonce_str,
            package: wxpayPackage,
            signType: 'MD5',
            paySign: paySign,
            Order_Type: Order_Type
          };
          this.wxPay(transData);
        }
      },
      fail: res => {
        if (this.globalData.order_action == 'pay_orderlist') {
          this.change_orderlist_variable();
        }
        wx.showToast({
          icon: 'loading',
          title: '服务器忙'
        })
      }
    })
  },

  //微信支付API
  wxPay: function(transData) {
    var that = this;
    wx.requestPayment({
      timeStamp: transData.timeStamp,
      nonceStr: transData.nonceStr,
      package: transData.package,
      signType: transData.signType,
      paySign: transData.paySign,
      success: function(res) {
        if (res.errMsg == 'requestPayment:ok') { //如果支付成功,跳转到订单列表页面
          // 处理分销商
          that.deal_distribute();

          //分销订单支付
          if (transData.Order_Type == 'distribute') {
            var pages = getCurrentPages(); //获取来源页面堆栈
            var currPage = pages[pages.length - 1]; //当前页面
            currPage.setData({
              is_dis: ''
            });
            currPage.onShow();
            return false;
          }

          if (that.globalData.order_action == 'pay_orderlist') {
            that.change_orderlist_variable(2);
          }
          that.globalData.show_order_status = 2; //用于跳转到的订单页的页面状态显示
          that.globalData.order_action = null; //清除订单操作
          wx.redirectTo({
            url: '/pages/orderlist/orderlist?order_type=' + that.globalData.order_type
          });
        }
      },
      fail: function(res) { //如果用户关闭支付窗口,询问是否确定取消
        if (res.errMsg != 'requestPayment:fail cancel') {
          var msg = res.errMsg.replace('requestPayment:fail ', '');
          wx.showModal({
            title: '提示',
            content: msg ? msg : '调取支付失败，请稍后重试',
            showCancel: false
          });
          return false;
        }
        if (that.globalData.order_action == 'pay_checkout') { //提交订单页面支付
          if (res.errMsg == 'requestPayment:fail cancel') {
            wx.showModal({
              title: '确定取消支付?',
              content: '好货不等人,确认取消支付吗?',
              cancelText: '取消支付',
              confirmText: '继续支付',
              success: function(res) {
                if (res.confirm) { //继续支付
                  that.globalData.callback && that.globalData.callback({
                    status: 1,
                    errorCode: 0,
                    action: 'callback'
                  });
                  that.globalData.callback = null;
                } else if (res.cancel) { //取消跳转到待付款页面
                  that.globalData.show_order_status = 1; //用于跳转到的订单页的页面状态显示
                  that.globalData.order_action = null; //清除订单操作
                  wx.redirectTo({
                    url: '/pages/orderlist/orderlist?order_type=' + that.globalData.order_type
                  });
                }
              }
            });
          }
        } else {
          if (that.globalData.order_action == 'pay_orderlist') {
            that.change_orderlist_variable();
          }
          that.globalData.order_action = null; //清除订单操作
        }
      },
      complete: function(res) {
        if (that.globalData.order_action == 'pay_checkout') { //提交订单页面支付
          if (res.errMsg == 'requestPayment:cancel') {
            wx.showModal({
              title: '确定取消支付?',
              content: '好货不等人,确认取消支付吗?',
              cancelText: '取消支付',
              confirmText: '继续支付',
              success: function(res) {
                if (res.confirm) { //继续支付
                  that.globalData.callback && that.globalData.callback({
                    status: 1,
                    errorCode: 0,
                    action: 'callback'
                  });
                  that.globalData.callback = null;
                } else if (res.cancel) { //取消跳转到待付款页面
                  that.globalData.show_order_status = 1; //用于跳转到的订单页的页面状态显示
                  that.globalData.order_action = null; //清除订单操作
                  wx.redirectTo({
                    url: '/pages/orderlist/orderlist?order_type=' + that.globalData.order_type
                  });
                }
              }
            });
          }
        } else {
          that.change_orderlist_variable();
        }
      }
    })
  },

  //改变orderlist页面的变量
  change_orderlist_variable: function(change_order_status) {
    var pages = getCurrentPages(); //获取来源页面堆栈
    var currPage = pages[pages.length - 1]; //当前页面
    var action_order_id = currPage.data.action_order_id;
    //1: 待付款  2：待发货  3：待收货  4：已完成
    if (change_order_status && '2,4'.indexOf(change_order_status) > -1) {
      var orderlist = currPage.data.orderlist;
      var flag = 0;
      for (var i in orderlist) {
        if (orderlist[i]['Order_ID'] == action_order_id) {
          if (orderlist[i]['Order_Status'] == 1 && change_order_status == 2) {
            orderlist[i]['Order_Status'] = 2;
            flag++;
          } else if (orderlist[i]['Order_Status'] == 3 && change_order_status == 4) {
            orderlist[i]['Order_Status'] = 4;
            flag++;
          }
        }
      }
      if (flag > 0) {
        currPage.setData({
          orderlist: orderlist
        });
      }
    }
    currPage.setData({
      order_action: null,
      action_order_id: 0
    });
  },

  //确认收货回调
  confirmReceive: function(res) {
    wx.hideLoading();
    var that = this;
    if (res.status == 1) {
      wx.showToast({
        title: '确认收货成功',
        icon: 'success',
        duration: 1500,
        complete: function() {
          // 处理分销商
          that.deal_distribute();

          that.change_orderlist_variable(4);
          that.globalData.show_order_status = 4; //用于跳转到的订单页的页面状态显示
          wx.redirectTo({
            url: '/pages/orderlist/orderlist?order_type=' + that.globalData.order_type
          });
        }
      });
    } else {
      that.change_orderlist_variable();
      wx.showModal({
        title: '提示',
        content: res.msg,
        showCancel: false
      });
    }
  },
  //------------订单操作  end--------------//

  //------------接口请求  start--------------//
  //请求服务器获取数据,需要四个参数,第一个传递的值,第二个请求的URL,第三个使用的方法,第四个成功后的回调函数
  http_req: function(args, url, method, callback = '', otherArgs = '') {
    args['appid'] = this.globalData.init.appid;

    let header = {};
    // 本地测试
    header['content-type'] = 'application/x-www-form-urlencoded';
    url = this.globalData.init.front_url;
    if (args.type && args.type == 'cart') {
      url += '/api/' + (args.UsersID ? args.UsersID : this.globalData.userInfo.Users_ID) + '/shop/cart/ajax/';
    } else if (args.type && args.type == 'confirm') {
      url += '/api/' + (args.UsersID ? args.UsersID : this.globalData.userInfo.Users_ID) + '/shop/member/ajax/';
    } else if (args.type && args.type == 'distribute') {
      url += '/api/distribute/distribute.php?UsersID=' + (args.UsersID ? args.UsersID : this.globalData.userInfo.Users_ID);
    } else if (args.type && args.type == 'web_article') {
      url += '/api/web/article_column.php';
    } else {
      url += args.uri ? args.uri : '/api/little_program/shopconfig.php';
    }

    var data = this.createToken(args);
    console.log(url);
    console.log(data);
    wx.request({
      method: method,
      header: header,
      url: url,
      data: data,
      success: res => {
        console.log(res.data);
        callback != '' && callback(res.data, otherArgs);
      }
    })
  },
  //创建签名
  createToken: function(object) {
    var signString = this.ObjectToString(object);
    signString = signString.slice(0, -1);
    var timestamp = parseInt(new Date().getTime() / 1000).toString();
    var key = 'NKJDhjkd5467jgef_fdj34#$#5713ds_54nabbas%$sad756_!';
    var base64 = new fun_base64.Base64();
    var data = signString + key + timestamp;
    var sign = md5.hexMD5(base64.encode(data)).toUpperCase();
    object['timestamp'] = timestamp;
    object['sign'] = sign;
    return object;
  },
  //对象转字符串
  ObjectToString: function(object) {
    var arrs = '';
    for (var i in object) {
      if (typeof object[i] != 'object') {
        if (object[i] !== '') {
          if (i != 'timestamp' && i != 'sign') {
            arrs += object[i] + ',';
          }
        }
      } else {
        var dis = this.ObjectToString(object[i]);
        arrs += dis;
      }
    }
    return arrs;
  },
  //------------接口请求  end--------------//
  //设置导航的文字和背景颜色
  setNavBarColor: function() {
    let NavBarFontColor = this.globalData.NavBarFontColor;
    let NavBarBgColor = this.globalData.NavBarBgColor;
    if (NavBarFontColor && NavBarFontColor != '' && NavBarBgColor && NavBarBgColor != '') {
      wx.setNavigationBarColor({
        frontColor: NavBarFontColor,
        backgroundColor: NavBarBgColor
      });
    }
  },

  //设置页面标题
  setNavBarTitle: function(title) {
    if (title && title != '') {
      wx.setNavigationBarTitle({
        title: title
      });
    }
  },
  //公共数据
  globalData: {
    init: null,
    shopConfig: null, //店铺配置
    expiresTime: (new Date().getTime() / 1000) + 10, //商城过期时间
    userInfo: null, //用户信息
    ownerid: 0, //初始化上级ID为0，用于用户注册
    callback: null, //登录后的回调
    show_order_status: -1, //用于跳转到的订单页的页面状态显示 （0，1，2，3，4）
    order_action: null, //订单操作 pay_checkout(提交订单页支付), pay_orderlist(订单列表页支付), confirm(确认收货)
    order_type: null, //订单类型  用于订单跳转
    appConfig: null //初始化ext.json
  }
});